using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;
namespace DataAccess
{
	/// <summary>
	/// Strongly-typed collection for the TblTask class.
	/// </summary>
    [Serializable]
	public partial class TblTaskCollection : ActiveList<TblTask, TblTaskCollection>
	{	   
		public TblTaskCollection() {}
        
        /// <summary>
		/// Filters an existing collection based on the set criteria. This is an in-memory filter
		/// Thanks to developingchris for this!
        /// </summary>
        /// <returns>TblTaskCollection</returns>
		public TblTaskCollection Filter()
        {
            for (int i = this.Count - 1; i > -1; i--)
            {
                TblTask o = this[i];
                foreach (SubSonic.Where w in this.wheres)
                {
                    bool remove = false;
                    System.Reflection.PropertyInfo pi = o.GetType().GetProperty(w.ColumnName);
                    if (pi.CanRead)
                    {
                        object val = pi.GetValue(o, null);
                        switch (w.Comparison)
                        {
                            case SubSonic.Comparison.Equals:
                                if (!val.Equals(w.ParameterValue))
                                {
                                    remove = true;
                                }
                                break;
                        }
                    }
                    if (remove)
                    {
                        this.Remove(o);
                        break;
                    }
                }
            }
            return this;
        }
		
		
	}
	/// <summary>
	/// This is an ActiveRecord class which wraps the TblTasks table.
	/// </summary>
	[Serializable]
	public partial class TblTask : ActiveRecord<TblTask>, IActiveRecord
	{
		#region .ctors and Default Settings
		
		public TblTask()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}
		
		private void InitSetDefaults() { SetDefaults(); }
		
		public TblTask(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}
        
		public TblTask(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}
		 
		public TblTask(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}
		
		protected static void SetSQLProps() { GetTableSchema(); }
		
		#endregion
		
		#region Schema and Query Accessor	
		public static Query CreateQuery() { return new Query(Schema); }
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}
		}
		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("TblTasks", TableType.Table, DataService.GetInstance("mainConnection"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarId = new TableSchema.TableColumn(schema);
				colvarId.ColumnName = "ID";
				colvarId.DataType = DbType.Guid;
				colvarId.MaxLength = 0;
				colvarId.AutoIncrement = false;
				colvarId.IsNullable = false;
				colvarId.IsPrimaryKey = true;
				colvarId.IsForeignKey = false;
				colvarId.IsReadOnly = false;
				colvarId.DefaultSetting = @"";
				colvarId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarId);
				
				TableSchema.TableColumn colvarUserName = new TableSchema.TableColumn(schema);
				colvarUserName.ColumnName = "UserName";
				colvarUserName.DataType = DbType.String;
				colvarUserName.MaxLength = -1;
				colvarUserName.AutoIncrement = false;
				colvarUserName.IsNullable = true;
				colvarUserName.IsPrimaryKey = false;
				colvarUserName.IsForeignKey = false;
				colvarUserName.IsReadOnly = false;
				colvarUserName.DefaultSetting = @"";
				colvarUserName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarUserName);
				
				TableSchema.TableColumn colvarAdvisorStepId = new TableSchema.TableColumn(schema);
				colvarAdvisorStepId.ColumnName = "AdvisorStepId";
				colvarAdvisorStepId.DataType = DbType.Guid;
				colvarAdvisorStepId.MaxLength = 0;
				colvarAdvisorStepId.AutoIncrement = false;
				colvarAdvisorStepId.IsNullable = true;
				colvarAdvisorStepId.IsPrimaryKey = false;
				colvarAdvisorStepId.IsForeignKey = true;
				colvarAdvisorStepId.IsReadOnly = false;
				colvarAdvisorStepId.DefaultSetting = @"";
				
					colvarAdvisorStepId.ForeignKeyTableName = "TblStepsAdvisor";
				schema.Columns.Add(colvarAdvisorStepId);
				
				TableSchema.TableColumn colvarContractId = new TableSchema.TableColumn(schema);
				colvarContractId.ColumnName = "ContractId";
				colvarContractId.DataType = DbType.Guid;
				colvarContractId.MaxLength = 0;
				colvarContractId.AutoIncrement = false;
				colvarContractId.IsNullable = true;
				colvarContractId.IsPrimaryKey = false;
				colvarContractId.IsForeignKey = true;
				colvarContractId.IsReadOnly = false;
				colvarContractId.DefaultSetting = @"";
				
					colvarContractId.ForeignKeyTableName = "TblContract";
				schema.Columns.Add(colvarContractId);
				
				TableSchema.TableColumn colvarDateX = new TableSchema.TableColumn(schema);
				colvarDateX.ColumnName = "Date";
				colvarDateX.DataType = DbType.DateTime;
				colvarDateX.MaxLength = 0;
				colvarDateX.AutoIncrement = false;
				colvarDateX.IsNullable = true;
				colvarDateX.IsPrimaryKey = false;
				colvarDateX.IsForeignKey = false;
				colvarDateX.IsReadOnly = false;
				colvarDateX.DefaultSetting = @"";
				colvarDateX.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDateX);
				
				TableSchema.TableColumn colvarSurveyorStepId = new TableSchema.TableColumn(schema);
				colvarSurveyorStepId.ColumnName = "SurveyorStepId";
				colvarSurveyorStepId.DataType = DbType.Guid;
				colvarSurveyorStepId.MaxLength = 0;
				colvarSurveyorStepId.AutoIncrement = false;
				colvarSurveyorStepId.IsNullable = true;
				colvarSurveyorStepId.IsPrimaryKey = false;
				colvarSurveyorStepId.IsForeignKey = true;
				colvarSurveyorStepId.IsReadOnly = false;
				colvarSurveyorStepId.DefaultSetting = @"";
				
					colvarSurveyorStepId.ForeignKeyTableName = "TblStepsSurveyor";
				schema.Columns.Add(colvarSurveyorStepId);
				
				TableSchema.TableColumn colvarDelayDays = new TableSchema.TableColumn(schema);
				colvarDelayDays.ColumnName = "DelayDays";
				colvarDelayDays.DataType = DbType.Int32;
				colvarDelayDays.MaxLength = 0;
				colvarDelayDays.AutoIncrement = false;
				colvarDelayDays.IsNullable = true;
				colvarDelayDays.IsPrimaryKey = false;
				colvarDelayDays.IsForeignKey = false;
				colvarDelayDays.IsReadOnly = false;
				colvarDelayDays.DefaultSetting = @"";
				colvarDelayDays.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDelayDays);
				
				TableSchema.TableColumn colvarStatus = new TableSchema.TableColumn(schema);
				colvarStatus.ColumnName = "Status";
				colvarStatus.DataType = DbType.String;
				colvarStatus.MaxLength = -1;
				colvarStatus.AutoIncrement = false;
				colvarStatus.IsNullable = true;
				colvarStatus.IsPrimaryKey = false;
				colvarStatus.IsForeignKey = false;
				colvarStatus.IsReadOnly = false;
				colvarStatus.DefaultSetting = @"";
				colvarStatus.ForeignKeyTableName = "";
				schema.Columns.Add(colvarStatus);
				
				TableSchema.TableColumn colvarIsDonei = new TableSchema.TableColumn(schema);
				colvarIsDonei.ColumnName = "IsDonei";
				colvarIsDonei.DataType = DbType.Boolean;
				colvarIsDonei.MaxLength = 0;
				colvarIsDonei.AutoIncrement = false;
				colvarIsDonei.IsNullable = true;
				colvarIsDonei.IsPrimaryKey = false;
				colvarIsDonei.IsForeignKey = false;
				colvarIsDonei.IsReadOnly = false;
				colvarIsDonei.DefaultSetting = @"";
				colvarIsDonei.ForeignKeyTableName = "";
				schema.Columns.Add(colvarIsDonei);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["mainConnection"].AddSchema("TblTasks",schema);
			}
		}
		#endregion
		
		#region Props
		  
		[XmlAttribute("Id")]
		[Bindable(true)]
		public Guid Id 
		{
			get { return GetColumnValue<Guid>(Columns.Id); }
			set { SetColumnValue(Columns.Id, value); }
		}
		  
		[XmlAttribute("UserName")]
		[Bindable(true)]
		public string UserName 
		{
			get { return GetColumnValue<string>(Columns.UserName); }
			set { SetColumnValue(Columns.UserName, value); }
		}
		  
		[XmlAttribute("AdvisorStepId")]
		[Bindable(true)]
		public Guid? AdvisorStepId 
		{
			get { return GetColumnValue<Guid?>(Columns.AdvisorStepId); }
			set { SetColumnValue(Columns.AdvisorStepId, value); }
		}
		  
		[XmlAttribute("ContractId")]
		[Bindable(true)]
		public Guid? ContractId 
		{
			get { return GetColumnValue<Guid?>(Columns.ContractId); }
			set { SetColumnValue(Columns.ContractId, value); }
		}
		  
		[XmlAttribute("DateX")]
		[Bindable(true)]
		public DateTime? DateX 
		{
			get { return GetColumnValue<DateTime?>(Columns.DateX); }
			set { SetColumnValue(Columns.DateX, value); }
		}
		  
		[XmlAttribute("SurveyorStepId")]
		[Bindable(true)]
		public Guid? SurveyorStepId 
		{
			get { return GetColumnValue<Guid?>(Columns.SurveyorStepId); }
			set { SetColumnValue(Columns.SurveyorStepId, value); }
		}
		  
		[XmlAttribute("DelayDays")]
		[Bindable(true)]
		public int? DelayDays 
		{
			get { return GetColumnValue<int?>(Columns.DelayDays); }
			set { SetColumnValue(Columns.DelayDays, value); }
		}
		  
		[XmlAttribute("Status")]
		[Bindable(true)]
		public string Status 
		{
			get { return GetColumnValue<string>(Columns.Status); }
			set { SetColumnValue(Columns.Status, value); }
		}
		  
		[XmlAttribute("IsDonei")]
		[Bindable(true)]
		public bool? IsDonei 
		{
			get { return GetColumnValue<bool?>(Columns.IsDonei); }
			set { SetColumnValue(Columns.IsDonei, value); }
		}
		
		#endregion
		
		
		#region PrimaryKey Methods		
		
        protected override void SetPrimaryKey(object oValue)
        {
            base.SetPrimaryKey(oValue);
            
            SetPKValues();
        }
        
		
		public DataAccess.TblFileCollection TblFiles()
		{
			return new DataAccess.TblFileCollection().Where(TblFile.Columns.TaskId, Id).Load();
		}
		#endregion
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a TblContract ActiveRecord object related to this TblTask
		/// 
		/// </summary>
		public DataAccess.TblContract TblContract
		{
			get { return DataAccess.TblContract.FetchByID(this.ContractId); }
			set { SetColumnValue("ContractId", value.Id); }
		}
		
		
		/// <summary>
		/// Returns a TblStepsAdvisor ActiveRecord object related to this TblTask
		/// 
		/// </summary>
		public DataAccess.TblStepsAdvisor TblStepsAdvisor
		{
			get { return DataAccess.TblStepsAdvisor.FetchByID(this.AdvisorStepId); }
			set { SetColumnValue("AdvisorStepId", value.Id); }
		}
		
		
		/// <summary>
		/// Returns a TblStepsSurveyor ActiveRecord object related to this TblTask
		/// 
		/// </summary>
		public DataAccess.TblStepsSurveyor TblStepsSurveyor
		{
			get { return DataAccess.TblStepsSurveyor.FetchByID(this.SurveyorStepId); }
			set { SetColumnValue("SurveyorStepId", value.Id); }
		}
		
		
		#endregion
		
		
		
		//no ManyToMany tables defined (0)
		
        
        
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(Guid varId,string varUserName,Guid? varAdvisorStepId,Guid? varContractId,DateTime? varDateX,Guid? varSurveyorStepId,int? varDelayDays,string varStatus,bool? varIsDonei)
		{
			TblTask item = new TblTask();
			
			item.Id = varId;
			
			item.UserName = varUserName;
			
			item.AdvisorStepId = varAdvisorStepId;
			
			item.ContractId = varContractId;
			
			item.DateX = varDateX;
			
			item.SurveyorStepId = varSurveyorStepId;
			
			item.DelayDays = varDelayDays;
			
			item.Status = varStatus;
			
			item.IsDonei = varIsDonei;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(Guid varId,string varUserName,Guid? varAdvisorStepId,Guid? varContractId,DateTime? varDateX,Guid? varSurveyorStepId,int? varDelayDays,string varStatus,bool? varIsDonei)
		{
			TblTask item = new TblTask();
			
				item.Id = varId;
			
				item.UserName = varUserName;
			
				item.AdvisorStepId = varAdvisorStepId;
			
				item.ContractId = varContractId;
			
				item.DateX = varDateX;
			
				item.SurveyorStepId = varSurveyorStepId;
			
				item.DelayDays = varDelayDays;
			
				item.Status = varStatus;
			
				item.IsDonei = varIsDonei;
			
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		#endregion
        
        
        
        #region Typed Columns
        
        
        public static TableSchema.TableColumn IdColumn
        {
            get { return Schema.Columns[0]; }
        }
        
        
        
        public static TableSchema.TableColumn UserNameColumn
        {
            get { return Schema.Columns[1]; }
        }
        
        
        
        public static TableSchema.TableColumn AdvisorStepIdColumn
        {
            get { return Schema.Columns[2]; }
        }
        
        
        
        public static TableSchema.TableColumn ContractIdColumn
        {
            get { return Schema.Columns[3]; }
        }
        
        
        
        public static TableSchema.TableColumn DateXColumn
        {
            get { return Schema.Columns[4]; }
        }
        
        
        
        public static TableSchema.TableColumn SurveyorStepIdColumn
        {
            get { return Schema.Columns[5]; }
        }
        
        
        
        public static TableSchema.TableColumn DelayDaysColumn
        {
            get { return Schema.Columns[6]; }
        }
        
        
        
        public static TableSchema.TableColumn StatusColumn
        {
            get { return Schema.Columns[7]; }
        }
        
        
        
        public static TableSchema.TableColumn IsDoneiColumn
        {
            get { return Schema.Columns[8]; }
        }
        
        
        
        #endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string Id = @"ID";
			 public static string UserName = @"UserName";
			 public static string AdvisorStepId = @"AdvisorStepId";
			 public static string ContractId = @"ContractId";
			 public static string DateX = @"Date";
			 public static string SurveyorStepId = @"SurveyorStepId";
			 public static string DelayDays = @"DelayDays";
			 public static string Status = @"Status";
			 public static string IsDonei = @"IsDonei";
						
		}
		#endregion
		
		#region Update PK Collections
		
        public void SetPKValues()
        {
}
        #endregion
    
        #region Deep Save
		
        public void DeepSave()
        {
            Save();
            
}
        #endregion
	}
}
